//
//  ViewController.swift
//  SQLite
//
//  Created by 武莎莎 on 15/9/17.
//  Copyright © 2015年 itcast. All rights reserved.
//

import UIKit
// 在Swift中使用OC代码，需要添加桥接文件
class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
    
        batchUpdate()
    }
    
    func demoQueue() {
        let start = CFAbsoluteTimeGetCurrent()
        let sql = "INSERT INTO W_Student (name,sex,score) VALUES (?,?,?);"
        SQLiteManager.sharedManager.queueUpdate { (manager) -> () in
            for i in 0..<10000{
                if !manager.prepareUpdate(sql, args: "wss\(i)","male",99) {
                    SQLiteManager.sharedManager.rollbackTransaction()
                    break
                }
            }
            print(NSThread.currentThread())
            print(CFAbsoluteTimeGetCurrent() - start)
        }
    }
    // 预编译SQL更新
    func prepareSQLupdate() {
        // 预编译SQL中不需要使用''
        // 首先预编译SQL语句，然后再执行的时候再动态绑定 ？对应的数值，执行效率更高
        let sql = "INSERT INTO W_Student (name,sex,score) VALUES (?,?,?);"
        SQLiteManager.sharedManager.prepareUpdate(sql, args: "lisi","male",98)
    }
    // 批量更新
    func batchUpdate() {
        let start = CFAbsoluteTimeGetCurrent()
        
        SQLiteManager.sharedManager.beginTransaction()
        let sql = "INSERT INTO W_Student (name,sex,score) VALUES (?,?,?);"
        for i in 0..<10000 {
            
            if !SQLiteManager.sharedManager.prepareUpdate(sql, args: "lisi\(i)","male",91){
                SQLiteManager.sharedManager.rollbackTransaction()
            }
            
            // 模拟错误
            if i == 100 {
                SQLiteManager.sharedManager.rollbackTransaction()
                break // 跳出循环
            }
        }
        SQLiteManager.sharedManager.commitTransaction()
        print(CFAbsoluteTimeGetCurrent() - start)
    }

    func demoDeleteAll() {
        let sql = "DELETE FROM W_Student "
        SQLiteManager.sharedManager.execSQL(sql)
    }
    
    func demoinsert(){
        
        let dict = ["name":"ss","sex":"male","score":99.0]
        if Student(dict: dict).insertStudent() {
            print("插入数据成功")
        }else {
            print("插入数据失败")
        }
    }
    
    func demoupdate(){
        let dict = ["name":"miaohua","sex":"female","score":88,"number":2]
        if Student(dict: dict).updateStudent() {
            print("更新信息成功")
        }else{
            print("更新信息失败")
        }
    }
    
    func demodelete() {
        let dict = ["number":1]
        
        if Student(dict: dict).deleteStudent() {
            print("删除成功")
        }else {
            print("删除失败")
        }
    }
    /* 使用SQLite上传大批数据的时候，使用事务处理:
        优点：*安全，可以通过rollBack回到上一次的状态（防止数据传输过程中出现故障，导致的数据混乱）
             *效率高，提高性能
        ** 如果不主动开启事务的话，在执行一条insert/update语句就会开启一个新的事务，很耗性能
        ** 如果主动开启事务的话，就只会开启一个事务
    */
    func insertManyPerson() {
        // 开启事务
        SQLiteManager.sharedManager.execSQL("BEGIN TRANSACTION")
        
        let start = CFAbsoluteTimeGetCurrent()
        var index = 0
        for i in 0..<100 {
            let dict = ["name":"ld\(i)","sex":"male\(100 - i)","score":99.0]
            Student(dict: dict as! [String : AnyObject]).insertStudent()
            index++
        }
        // 关闭事务
        SQLiteManager.sharedManager.execSQL("COMMIT TRANSACTION")
        print(CFAbsoluteTimeGetCurrent() - start)
    }

}


